var express = require('express');
var router = express.Router();
const shangjiaxinxi = require("../public/商家信息.json")
const address = require("../public/address.json")
const users = require("../bin/DAO/users.js");
const dingdan =require("../bin/DAO/dingdan")
router.get('/',function(req,res,next){
    res.send('成功打开');
    return
});

//注册加登录
router.post('/login', function (req, res, next) {
    users.find({phone:req.body.data.phone},function(err,data){
        if (data.length!==0) {
            res.json({
                'msg': data,
            });
            return
        }
        else{
            var msg_list = [];
            var msg = {phone: req.body.data.phone,nickname: '', address: [], password: '',dress: {
                    dressname:'',
                    dresssex: '',
                    dressphone:'',
                    dresslou: '',
                    dresssu: ''
                },province:'',city:'',school:''};
            var user=new users(msg);
            user.save(function(err){
                if(err){
                    console.log('出错了！！！'+err);
                    return
                }else{
                    msg.password=(msg.password!=='');
                    msg_list.push(msg);
                    res.json({
                        'msg':msg_list,
                    })
                    return
                }
            });
        }
    })

});

//验证码
router.post('/validation', function (req, res, next) {
    var num = parseInt(Math.random() * 9000 + 1000)
    console.log(num)
    res.json({
        'msg': num
    })
});
//增加地址
router.post('/address', function (req, res, next) {
    var item = req.body;
    users.find({phone:item['phone']},function(err,data){
    var newdress=data[0]['address']
    newdress.push({dressname:item['name'],dresssex:item['sex'],dressphone:item['dressphone'],dresslou:item['lou'],dresssu:item['su']});
    users.update({"phone":item['phone']},{"$set":{"address":newdress}},function(err,data){
        if(err){
            console.log(err)
            return
        }else{
            res.json({
                'msg': newdress
            });
            return
        }
    });
    });

    }
);
//当前地址
router.post('/dress',function(req,res,next){
   var item=req.body
    users.find({"phone":item['phone']},function(err,data){
        if(err){
            console.log(err)
        }else{
            users.update({"phone":item['phone']},{"$set":{"dress":data[0].address[Number(item['id'])]}},function(err,data){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        'msg':200
                    });
                    return
                }
            });
        }
    })
});
//省市
router.post('/city',function(req,res,next){
    var item=req.body;
    users.update({"phone":item['phone']},{"$set":{"province":item['province'],"city":item['city']}},function(err,data){
        if(err){
            console.log(err)
            return
        }else{
            res.json({
                'msg': 200
            });
            return
        }
    });
});
//学校
router.post('/school',function(req,res,next){
    var item=req.body;
    users.update({"phone":item['phone']},{"$set":{"school":item['school']}},function(err,data){
        if(err){
            console.log(err)
            return
        }else{
            res.json({
                'msg': 200
            });
            return
        }
    });
})
//更改用户名
router.post('/changename', function (req, res, next) {
    var phone=req.body.phone;
    var name=req.body.name;
    users.update({"phone":phone},{"$set":{"nickname":name}},function(err,data){
        if(err){
            console.log(err)
            return
        }else{
            console.log(data)
            res.json({
                msg: name
            })
            return
        }
    })
});
//更改手机号
router.post('/changephone', function (req, res, next) {
    var oldphone=req.body.oldphone;
    var newphone=req.body.newphone;
    users.find({phone:newphone},function(err,data){
        if(data.length==0){
            users.update({"phone":oldphone},{"$set":{"phone":newphone}},function(err,data){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        msg:1
                    })
                    return
                }
            })
        }else{
            res.json({
                    'msg':0
                });
                return
            }


    });

});
//更改密码
router.post('/changepassword', function (req, res, next) {
    var oldpwd=req.body.oldpwd;
    var newpwd=req.body.newpwd;
    var phone=req.body.phone;
    users.find({phone:phone},function(err,data){
        if(data[0].password==oldpwd){
            users.update({"phone":phone},{"$set":{"password":newpwd}},function(err,data){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        msg:true
                    })
                    return
                }
            })
        }else{
            res.json({
                'msg':0
            });
            return
        }
    });

});
//更改地址
router.post('/changedress', function (req, res, next) {
    var index=req.body.id;
    var newdress=req.body.newdress;
    var phone=req.body.phone;
    var sure=req.body.sure;
    users.find({'phone':phone},(err,data)=>{
        if(sure){
            var address=data[0].address;
            address[index]=newdress;
            users.update({"phone":phone},{"$set":{"address":address,'dress':newdress}},function(err){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        state:200,
                        msg:address,
                        id:index
                    });
                    return
                }
            });
        }else{
            var address=data[0].address;
            address[index]=newdress;
            users.update({"phone":phone},{"$set":{"address":address}},function(err){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        state:200,
                        msg:address,
                    });
                    return
                }
            });
        }
    })
});
//删除地址
router.post('/deldress', function (req, res, next) {
    var index=req.body.index;
    var phone=req.body.phone;
    var sure=req.body.sure;
    users.find({'phone':phone},(err,data)=>{
        var address=data[0].address;
        address.splice(index,1);
        if(sure){
            users.update({"phone":phone},{"$set":{"address":address,'dress': {
                        dressname: '',
                        dresssex: '',
                        dressphone:'',
                        dresslou: '',
                        dresssu: ''
                    }}},function(err){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        'msg':200
                    });
                    return
                }
            });
        }
        else{
            users.update({"phone":phone},{"$set":{"address":address}},function(err){
                if(err){
                    console.log(err)
                    return
                }else{
                    res.json({
                        'msg':200
                    });
                    return
                }
            });
        }
    })
});
router.post("/shuju",function(req,res){
    res.json(shangjiaxinxi)
    return
});
router.post("/msg",function(req,res){
    res.json(address)
    return
});
router.post("/Dingdan",function (req,res) {
    dingdan.find({phone:req.body.phone}).populate("userid").exec(function(err,data){
        if(err){
            console.log('查询出错')
            return
        }else{
            // res.render('index',{data:data});
            res.json({data:data})
            return
        }
    });
    })


router.post("/AddDingdan",function (req,res) {
    users.find({phone: req.body.phone}, function (err, data) {
        var userID = data._id
        var Dingdan = {
            ShopName: req.body.ShopName,
            userID:userID,
            phone:req.body.phone,
            shangjiaImage: req.body.shangjiaImage,
            ShopType: req.body.ShopType,
            Money: req.body.Money,
            Date: req.body.Date,
            OK: req.body.OK,
        };
        var qq = new dingdan(Dingdan);
        qq.save(function (err) {
            if (err) {
                console.log('出错了！！！' + err);
                return
            } else {
                res.json({
                    'msg': Dingdan
                });
                return
            }
        })

    });
})
router.post('/DelDingdan',function (req,res) {
    dingdan.remove({_id:req.body.id},function(err,data) {
        if (err) {
            res.json({'msg':404});
            return
        } else {
            res.json({'msg':200});
            return
        }
    })
});

module.exports = router;